// pages/person/withdrawCash/withdrawCash.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliAccount: '',
    aliRealName: '',
    balanceAmount: 0,
    buttons: [
      { id: 1, amount: '5',checked:false }, 
    { id: 2, amount: '10',checked:false }, 
    { id: 3, amount: '50',checked:false }, 
    { id: 4, amount: '100',checked:false }, 
    { id: 5, amount: '500',checked:false }, 
    { id: 6, amount: '1000',checked:false }
  ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.requestUserAccount();
  },
  // 单选
  radioButtonTap: function (e) {
    let id = e.currentTarget.dataset.id
    for (let i = 0; i < this.data.buttons.length; i++) {
      if (this.data.buttons[i].id == id) {
        //当前点击的位置为true即选中
        this.data.buttons[i].checked = true;
      } else {
        //其他的位置为false
        this.data.buttons[i].checked = false;
      }
    }
    this.setData({
      buttons: this.data.buttons
    })
  },
  //点击提交
  actionSure() {
    if (this.data.aliAccount.length == 0) {
      ui.showToast('请输入提现账号');
      return;
    }
    if (this.data.aliRealName.length == 0) {
      ui.showToast('请输入实名认证的姓名');
      return;
    }
    var selectAmount = 0;
    for (let i = 0; i < this.data.buttons.length; i++) {
      if (this.data.buttons[i].checked) {
        selectAmount = this.data.buttons[i].amount;
      }
    }
    if (selectAmount == 0) {
      ui.showToast('请选择提现金额');
      return;
    }
    let obj = {
      method: 'POST',
      contentType: 'application/x-www-form-urlencoded',
      url: '/user/' + wx.getStorageSync('USERDATA').platformId + '/withdraw-without-accountinfo',
      data: {
        channelAccount: this.data.aliAccount,
        realName: this.data.aliRealName,
        platformId: wx.getStorageSync('USERDATA').platformId,
        withdrawAmount: selectAmount
      }
    }
    httpUtils.request(obj).then(res => {
      wx.showModal({
        title: '提示',
        content: '提现成功（预计1-3个工作日内处理）',
        confirmText: '好的',
        complete: (res) => {
          if (res.confirm) {
            wx.navigateBack({
              delta: 0,
            });
          }
        }
      });
    }).catch(err => {
      console.log('ERROR')
    });
  },
  //请求用户账户信息
  requestUserAccount() {
    let obj = {
      method: 'GET',
      url: '/user/' + wx.getStorageSync('USERDATA').platformId + '/account-info'
    }
    httpUtils.request(obj).then(res => {
      this.setData({
        aliAccount: res.data.account,
        aliRealName: res.data.realName,
        balanceAmount: res.data.balanceAmount,
      })
      
    }).catch(err => {
      console.log('ERROR')
    });
  },
  //进入提现记录
  actionHistory() {
    wx.navigateTo({
      url: '/pages/person/withdrawCash/withdrawHistory',
    })
  },
  //联系客服
  actionKefu() {
    wx.openCustomerServiceChat({
      corpId: 'ww1a67d4324104daaa',
      extInfo: {
        url: 'https://work.weixin.qq.com/kfid/kfc967f9513998cc218',
      },
    })
  }
})